<?php


namespace App\Admin\Controllers;

use App\Models\AdminRolesModel;
use App\Models\AdminsModel;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AdminUserController extends Controller
{
    /**
     * 获取用户信息
     * @return bool
     */
    public function info()
    {
        $admin_info = AdminsModel::getAdminInfo($this->admin_info['id']);

        if (empty($admin_info)) {
            return $this->fail(CODE_ERR_AUTH);
        }

        return $this->success([
            'name' => $admin_info->username,
            'role_id' => $admin_info->role_id
        ]);
    }

    public function list()
    {
        $page_size = $this->request->post('size', 10);
        $page = $this->request->post('page', 1);
        $offset = ($page - 1) * $page_size;
        $offset = $offset > 0 ? $offset : 0;

        $admin_list = AdminsModel::query()->limit($page_size)->offset($offset)->get()->toArray();
        $total = AdminsModel::query()->count();

        $admin_role_ids = array_column($admin_list, 'role_id');
        $admin_roles = AdminRolesModel::query()->whereIn('id', $admin_role_ids)->get(['id', 'name'])->toArray();
        $admin_roles = array_column($admin_roles, null, 'id');

        foreach ($admin_list as &$list) {
            $list['role_name'] = $admin_roles[$list['role_id']]['name'];
        }

        return $this->success(['list' => $admin_list, 'total' => $total]);
    }

    public function addOrEdit()
    {
        $params = $this->request->all();
        $validator = Validator::make($params, [
            'username' => ['required', 'max:30'],
            'password' => ['max:255'],
            'role_id' => ['required', 'integer'],
            'is_admin' => ['required', 'boolean']
        ]);
        if ($validator->fails()) {
            return $this->fail(CODE_ERR_SYSTEM, $validator->errors()->first());
        }

        $data = [
            'username' => $params['username'],
            'role_id' => $params['role_id'],
            'is_admin' => isset($params['is_admin']) ? (int)$params['is_admin'] : NO,
        ];

        if (isset($params['password']) && !empty($params['password'])) {
            $data['password'] = Hash::make($params['password']);
        }

        if (!isset($params['id']) || empty($params['id'])) {
            if (!isset($data['password'])) {
                return $this->fail(CODE_ERR_SYSTEM, '请输入密码');
            }
            $re = AdminsModel::query()->insert($data);
        } else {
            $re = AdminsModel::query()->where('id', $params['id'])->update($data);
        }

        if (!$re) {
            return $this->fail(CODE_ERR_SYSTEM, '操作失败');
        }

        return $this->success();
    }

    public function del()
    {
        $id = $this->request->post('id', 0);

        if ($id == 0) {
            return $this->fail(CODE_ERR_PARAM,'菜单不存在');
        }

        $re = AdminsModel::query()->where('id', $id)->delete();

        if (!$re) {
            $this->fail(CODE_ERR_SYSTEM, '删除失败');
        }

        return $this->success();
    }
}
